package ChessBoard;

import java.util.ArrayList;


public class Pawn extends Figure {


	public Pawn(boolean isWhite, int[] position,ChessBoard board) {
		super(isWhite, position, board);
		super.value = 1;
	}

	@Override
	protected ArrayList<int[]> calculatePos() {
		super.resetAll();
		ArrayList<int[]> newMoves = new ArrayList<int[]>();
		int yAdd = -1;
		boolean moved = false;
		if (super.position[1] != 8 && super.position[1] != 1) {
			if (super.isWhite && super.position[1] != 2)
				moved = true;
			else if (!super.isWhite && super.position[1] != 7)
				moved = true;
			if (super.isWhite)
				yAdd = 1;
			if (super.correspondingBoard.fieldIsEmpty(new int[] { super.position[0],
					super.position[1] + yAdd })) {
				if (super.correspondingBoard.moveProvokesNoCheck(super.position[0], super.position[1]
						+ yAdd,this))
					newMoves.add(new int[] { super.position[0],
							super.position[1] + yAdd });
				yAdd = yAdd * 2;
				if (!moved
						&& super.correspondingBoard.fieldIsEmpty(new int[] { super.position[0],
								super.position[1] + yAdd })) {
					if (super.correspondingBoard.moveProvokesNoCheck(super.position[0],
							super.position[1] + yAdd,this))
						newMoves.add(new int[] { super.position[0],
								super.position[1] + yAdd });
				}
			}
		}
		calculateCoversAndKills();
		return newMoves;
	}

	protected void calculateCoversAndKills() {
		int yAdd = -1;
		if (super.isWhite)
			yAdd = 1;
		if (!super.correspondingBoard.fieldIsEmpty(new int[] { super.position[0] + 1,
				super.position[1] + yAdd })) {
			if (super.correspondingBoard.moveProvokesNoCheck(super.position[0], super.position[1] + yAdd,this)) {
				Figure f = super.correspondingBoard.getFigure(new int[] { super.position[0] + 1,
						super.position[1] + yAdd });
				if (f != null) {if (super.isWhite == f.isWhite) {
					if (f.value != 100)
						super.covers.add(f);
				} else {
						if (super.correspondingBoard.moveProvokesNoCheck(super.position[0] + 1,
								super.position[1] + yAdd,this)) {
							super.kills.add(f);
						}
				}}
			}
		}
		if (!super.correspondingBoard.fieldIsEmpty(new int[] { super.position[0] - 1,
				super.position[1] + yAdd })) {
			if (super.correspondingBoard.moveProvokesNoCheck(super.position[0], super.position[1] + yAdd,this)) {
				Figure f = super.correspondingBoard.getFigure(new int[] { super.position[0] - 1,
						super.position[1] + yAdd });
				if (f != null) {if (super.isWhite == f.isWhite) {
					if (f.value != 100)
						super.covers.add(f);
				} else {
						if (super.correspondingBoard.moveProvokesNoCheck(super.position[0] - 1,
								super.position[1] + yAdd,this)) {
							super.kills.add(f);
						}
				}}
			}
		}
	}

	@Override
	public void checkCheck() {
		if(this.kills.contains(super.correspondingBoard.getKing(!this.isWhite)))
			this.checks=true;
		else
			this.checks=false;
		
	}

}
